<?php

namespace Nimo\Bundle\BookstoreApiBundle\Entity\Book;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Nimo\Bundle\BookstoreApiBundle\Entity\Book\BookRepository")
 */
class Book
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $title
     *
     * @ORM\Column(name="title", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $title;

    /**
     * @var Author $author
     *
     * @ORM\ManyToOne(targetEntity="Author", inversedBy="books")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
     */
    private $author;

    /**
     * @var string $brief
     *
     * @ORM\Column(name="brief", type="text", nullable=true)
     */
    private $brief;

    /**
     * @var string $press
     *
     * @ORM\Column(name="press", type="text")
     */
    private $press;

    /**
     * @var string $meta
     *
     * @ORM\Column(name="meta", type="text")
     */
    private $meta;

    /**
     * @var string $publishedYear
     *
     * @ORM\Column(name="published_year", type="string", length=4)
     */
    private $publishedYear;

    /**
     * @var boolean $bestseller
     *
     * @ORM\Column(name="bestseller", type="boolean", nullable=true)
     */
    private $bestseller;

    /**
     * @var string $cover
     *
     * @ORM\Column(name="cover", type="string", length=255)
     */
    private $cover;

    /**
     * @var float $rating
     *
     * @ORM\Column(name="rating", type="float")
     */
    private $rating = 0.00;

    /**
     * @var float $price
     *
     * @ORM\Column(name="price", type="float")
     */
    private $price;

    /**
     * @var \DateTime $lastViewedDate
     *
     * @ORM\Column(name="last_viewed_date", type="datetime")
     */
    private $lastViewedDate;

    /**
     * @var boolean $isDraft
     *
     * @ORM\Column(name="is_draft", type="boolean", nullable=true)
     */
    private $isDraft;

    /**
     * @var Category $category
     *
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="books")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;

    /**
     * @var ArrayCollection $reviews
     *
     * @ORM\OneToMany(targetEntity="Nimo\Bundle\BookstoreApiBundle\Entity\User\Review", mappedBy="book")
     * @ORM\OrderBy({"createdAt" = "DESC"})
     */
    protected $reviews;

    /**
     * @var ArrayCollection $view_history
     *
     * @ORM\OneToMany(targetEntity="Nimo\Bundle\BookstoreApiBundle\Entity\User\ViewHistory", mappedBy="book")
     */
    protected $view_history;

    /**
     * @var integer $viewCount
     *
     * @ORM\Column(name="view_count", type="integer")
     */
    private $viewCount = 0;

    /**
     * @var integer $soldCount
     *
     * @ORM\Column(name="sold_count", type="integer")
     */
    private $soldCount = 0;

    /**
     * @var string $isbnCode
     *
     * @ORM\Column(name="isbn_code", type="string", length=255)
     */
    private $isbnCode = '';

    /**
     * @var integer $pageCount
     *
     * @ORM\Column(name="page_count", type="integer")
     */
    private $pageCount;

    /**
     * @var string $language
     *
     * @ORM\Column(name="language", type="string", length=64)
     */
    private $language = 'Монгол';

    /**
     * @var ArrayCollection $tags
     *
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="books")
     * @ORM\JoinTable(name="book_tags",
     *      joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     *      )
     */
    private $tags;

    /**
     * @ORM\ManyToMany(targetEntity="Book", mappedBy="related_books")
     */
    private $relating_books;

    /**
     * @var ArrayCollection $related_books
     *
     * @ORM\ManyToMany(targetEntity="Book", inversedBy="relating_books")
     * @ORM\JoinTable(name="related_book",
     *      joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="related_book_id", referencedColumnName="id")}
     *      )
     */
    private $related_books;

    public function toArray()
    {
        $entity = array();
        $entity['id']       = $this->getId();
        $entity['title']    = $this->getTitle();
        $entity['introduction'] = $this->getBrief();
        $entity['author']   = $this->getAuthor() !== null ? $this->getAuthor()->getName() : null;
        $entity['published_year'] = $this->getPublishedYear();
        $entity['is_bestseller']  = $this->isBestseller();
        $entity['cover']    = $this->getCover();
        $entity['rate']     = $this->getRating();
        $entity['price']    = $this->getPrice();
        $entity['last_viewed_at'] = date('Y-m-d H:i:s',$this->getLastViewedDate()->getTimestamp());
        $entity['view_count'] = $this->getViewCount();
        $entity['sold_count'] = $this->getSoldCount();
        $entity['isbn_code']  = $this->getIsbnCode();
        $entity['page_count'] = $this->getPageCount();
        $entity['category']   = $this->getCategory() !== null ? $this->getCategory()->getName() : null;
        $entity['tags']       = array();
        foreach($this->getTags() as $tag){
            $entity['tags'][] = $tag->getTag();
        }
        $entity['reviews']    = array();
        foreach($this->getReviews() as $review){
            $entity['reviews'][] = array('title'=>$review->getTitle(),'comment'=>$review->getReview(),'rate'=>$review->getScore());
        }

        return $entity;
    }

    public function increaseViewCount()
    {
        $this->viewCount += 1;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Book
     */
    public function setTitle($title)
    {
        $this->title = $title;
    
        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set Author
     *
     * @param Author $author
     * @return Book
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * get Author
     *
     * @return Author
     */
    public function getAuthor()
    {
        return $this->author;
    }

    /**
     * Set brief
     *
     * @param string $brief
     * @return Book
     */
    public function setBrief($brief)
    {
        $this->brief = $brief;
    
        return $this;
    }

    /**
     * Get brief
     *
     * @return string 
     */
    public function getBrief()
    {
        return $this->brief;
    }

    /**
     * Set press
     *
     * @param string $press
     * @return Book
     */
    public function setPress($press)
    {
        $this->press = $press;
    
        return $this;
    }

    /**
     * Get press
     *
     * @return string 
     */
    public function getPress()
    {
        return $this->press;
    }

    /**
     * Set meta
     *
     * @param string $meta
     * @return Book
     */
    public function setMeta($meta)
    {
        $this->meta = $meta;
    
        return $this;
    }

    /**
     * Get meta
     *
     * @return string 
     */
    public function getMeta()
    {
        return $this->meta;
    }

    /**
     * Set publishedYear
     *
     * @param string $publishedYear
     * @return Book
     */
    public function setPublishedYear($publishedYear)
    {
        $this->publishedYear = $publishedYear;
    
        return $this;
    }

    /**
     * Get publishedYear
     *
     * @return string 
     */
    public function getPublishedYear()
    {
        return $this->publishedYear;
    }

    /**
     * Set bestseller
     *
     * @param boolean $bestseller
     * @return Book
     */
    public function setBestseller($bestseller)
    {
        $this->bestseller = $bestseller;
    
        return $this;
    }

    /**
     * Get bestseller
     *
     * @return boolean 
     */
    public function getBestseller()
    {
        return $this->bestseller;
    }

    public function isBestseller()
    {
        return $this->getBestseller();
    }

    /**
     * Set cover
     *
     * @param string $cover
     * @return Book
     */
    public function setCover($cover)
    {
        $this->cover = $cover;
    
        return $this;
    }

    /**
     * Get cover
     *
     * @return string 
     */
    public function getCover()
    {
        return $this->cover;
    }

    /**
     * Set rating
     *
     * @param float $rating
     * @return Book
     */
    public function setRating($rating)
    {
        $this->rating = $rating;
    
        return $this;
    }

    /**
     * Get rating
     *
     * @return float 
     */
    public function getRating()
    {
        return $this->rating;
    }

    /**
     * Set price
     *
     * @param float $price
     * @return Book
     */
    public function setPrice($price)
    {
        $this->price = $price;
    
        return $this;
    }

    /**
     * Get price
     *
     * @return float 
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set lastViewedDate
     *
     * @param \DateTime $lastViewedDate
     * @return Book
     */
    public function setLastViewedDate($lastViewedDate)
    {
        $this->lastViewedDate = $lastViewedDate;
    
        return $this;
    }

    /**
     * Get lastViewedDate
     *
     * @return \DateTime 
     */
    public function getLastViewedDate()
    {
        return $this->lastViewedDate;
    }

    /**
     * Set isDraft
     *
     * @param boolean $isDraft
     * @return Book
     */
    public function setIsDraft($isDraft)
    {
        $this->isDraft = $isDraft;
    
        return $this;
    }

    /**
     * Get isDraft
     *
     * @return boolean 
     */
    public function getIsDraft()
    {
        return $this->isDraft;
    }

    /**
     * Set category
     *
     * @param Category $category
     * @return Book
     */
    public function setCategory($category)
    {
        $this->category = $category;
    
        return $this;
    }

    /**
     * Get category
     *
     * @return Category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set viewCount
     *
     * @param integer $viewCount
     * @return Book
     */
    public function setViewCount($viewCount)
    {
        $this->viewCount = $viewCount;
    
        return $this;
    }

    /**
     * Get viewCount
     *
     * @return integer 
     */
    public function getViewCount()
    {
        return $this->viewCount;
    }

    /**
     * Set soldCount
     *
     * @param integer $soldCount
     * @return Book
     */
    public function setSoldCount($soldCount)
    {
        $this->soldCount = $soldCount;
    
        return $this;
    }

    /**
     * Get soldCount
     *
     * @return integer 
     */
    public function getSoldCount()
    {
        return $this->soldCount;
    }

    /**
     * Set isbnCode
     *
     * @param string $isbnCode
     * @return Book
     */
    public function setIsbnCode($isbnCode)
    {
        $this->isbnCode = $isbnCode;
    
        return $this;
    }

    /**
     * Get isbnCode
     *
     * @return string 
     */
    public function getIsbnCode()
    {
        return $this->isbnCode;
    }

    /**
     * Set pageCount
     *
     * @param integer $pageCount
     * @return Book
     */
    public function setPageCount($pageCount)
    {
        $this->pageCount = $pageCount;
    
        return $this;
    }

    /**
     * Get pageCount
     *
     * @return integer 
     */
    public function getPageCount()
    {
        return $this->pageCount;
    }

    /**
     * Set language
     *
     * @param string $language
     * @return Book
     */
    public function setLanguage($language)
    {
        $this->language = $language;
    
        return $this;
    }

    /**
     * Get language
     *
     * @return string 
     */
    public function getLanguage()
    {
        return $this->language;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->tags = new ArrayCollection();
        $this->reviews = new ArrayCollection();
        $this->view_history = new ArrayCollection();
        $this->related_books = new ArrayCollection();
        $this->relating_books = new ArrayCollection();
    }
    
    /**
     * Add tags
     *
     * @param Tag $tags
     * @return Book
     */
    public function addTag(Tag $tags)
    {
        $this->tags[] = $tags;
    
        return $this;
    }

    /**
     * Remove tags
     *
     * @param Tag $tags
     */
    public function removeTag(Tag $tags)
    {
        $this->tags->removeElement($tags);
    }

    /**
     * Get tags
     *
     * @return Collection
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * Add reviews
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\User\Review $reviews
     * @return Book
     */
    public function addReview(\Nimo\Bundle\BookstoreApiBundle\Entity\User\Review $reviews)
    {
        $this->reviews[] = $reviews;
    
        return $this;
    }

    /**
     * Remove reviews
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\User\Review $reviews
     */
    public function removeReview(\Nimo\Bundle\BookstoreApiBundle\Entity\User\Review $reviews)
    {
        $this->reviews->removeElement($reviews);
    }

    /**
     * Get reviews
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getReviews()
    {
        return $this->reviews;
    }

    /**
     * Add view_history
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\User\ViewHistory $viewHistory
     * @return Book
     */
    public function addViewHistory(\Nimo\Bundle\BookstoreApiBundle\Entity\User\ViewHistory $viewHistory)
    {
        $this->view_history[] = $viewHistory;
    
        return $this;
    }

    /**
     * Remove view_history
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\User\ViewHistory $viewHistory
     */
    public function removeViewHistory(\Nimo\Bundle\BookstoreApiBundle\Entity\User\ViewHistory $viewHistory)
    {
        $this->view_history->removeElement($viewHistory);
    }

    /**
     * Get view_history
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getViewHistory()
    {
        return $this->view_history;
    }

    /**
     * Add relating_books
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatingBooks
     * @return Book
     */
    public function addRelatingBook(\Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatingBooks)
    {
        $this->relating_books[] = $relatingBooks;
    
        return $this;
    }

    /**
     * Remove relating_books
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatingBooks
     */
    public function removeRelatingBook(\Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatingBooks)
    {
        $this->relating_books->removeElement($relatingBooks);
    }

    /**
     * Get relating_books
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getRelatingBooks()
    {
        return $this->relating_books;
    }

    /**
     * Add related_books
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatedBooks
     * @return Book
     */
    public function addRelatedBook(\Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatedBooks)
    {
        $this->related_books[] = $relatedBooks;
    
        return $this;
    }

    /**
     * Remove related_books
     *
     * @param Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatedBooks
     */
    public function removeRelatedBook(\Nimo\Bundle\BookstoreApiBundle\Entity\Book\Book $relatedBooks)
    {
        $this->related_books->removeElement($relatedBooks);
    }

    /**
     * Get related_books
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getRelatedBooks()
    {
        return $this->related_books;
    }
}